package com.sjz.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;


/****
 * 事物工具类
 *
 */
@Component
public class TransactionManager {

	
	/***
	 * 这个是通过配置类来设置bean  这个来得到这个DataSourceTransactionManager
	 */
	@Autowired
	private DataSourceTransactionManager dataSourceTransactionManager;

	/**
	 * @methodDesc: 功能描述:(开启事物)
	 */
	public TransactionStatus begin() {
		// 获取到事物状态
		return dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
	}

	/**
	 * 
	 * @methodDesc: 功能描述:(提交事物)
	 */
	public void commit(TransactionStatus transactionStatus) {
		dataSourceTransactionManager.commit(transactionStatus); // 数据会提交到数据库中...

	}

	/**
	 * 
	 * @methodDesc: 功能描述:(发生异常,数据库要回滚)
	 */
	public void rollback(TransactionStatus transactionStatus) {
		dataSourceTransactionManager.rollback(transactionStatus);
	}

}
